package com.xiaoyu.lanling.feature.chat.util;

import android.graphics.drawable.AnimationDrawable;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.xiaoyu.im.datamodel.AppMessage;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.event.chat.ChatReceiveAudioPlayComplementEvent;
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider;
import com.xiaoyu.lanling.util.MP3PlayerUtils;

public class AudioViewHolderAnimUtils {

    private static final AudioViewHolderAnimUtils ourInstance = new AudioViewHolderAnimUtils();

    private AnimationDrawable mReceiveAnimationDrawable;
    private AnimationDrawable mSendAnimationDrawable;

    public static AudioViewHolderAnimUtils getInstance() {
        return ourInstance;
    }

    private AudioViewHolderAnimUtils() {
    }

    public void play(AppMessage message, String audioFilePath, TextView textView, boolean isReceive) {
        if (mReceiveAnimationDrawable != null) {
            mReceiveAnimationDrawable.stop();
            mReceiveAnimationDrawable.selectDrawable(0);
        }
        if (mSendAnimationDrawable != null) {
            mSendAnimationDrawable.stop();
            mSendAnimationDrawable.selectDrawable(0);
        }

        if (MP3PlayerUtils.getInstance().isPlaying(audioFilePath)) {
            MP3PlayerUtils.getInstance().stop();
            return;
        }

        if (isReceive) {
            mReceiveAnimationDrawable = (AnimationDrawable) ContextCompat.getDrawable(textView.getContext(), R.drawable.anim_chat_receive_audio);
            textView.setCompoundDrawablesWithIntrinsicBounds(mReceiveAnimationDrawable, null, null, null);
            boolean isPlaying = MP3PlayerUtils.getInstance().start(audioFilePath, mp -> {
                MP3PlayerUtils.getInstance().stop();
                mReceiveAnimationDrawable.stop();
                mReceiveAnimationDrawable.selectDrawable(0);
                new ChatReceiveAudioPlayComplementEvent(message).post();
            });

            if (isPlaying) {
                mReceiveAnimationDrawable.start();
            } else {
                mReceiveAnimationDrawable.stop();
                mReceiveAnimationDrawable.selectDrawable(0);
            }

            updateNewTip(message);
        } else {
            mSendAnimationDrawable = (AnimationDrawable) ContextCompat.getDrawable(textView.getContext(), R.drawable.anim_chat_send_audio);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, mSendAnimationDrawable, null);
            boolean isPlaying = MP3PlayerUtils.getInstance().start(audioFilePath, mp -> {
                MP3PlayerUtils.getInstance().stop();
                mSendAnimationDrawable.stop();
                mSendAnimationDrawable.selectDrawable(0);
            });

            if (isPlaying) {
                mSendAnimationDrawable.start();
            } else {
                mSendAnimationDrawable.stop();
                mSendAnimationDrawable.selectDrawable(0);
            }
        }
    }

    private void updateNewTip(AppMessage message) {
        if (!message.hasNewTip()) {
            return;
        }
        MessageDataProvider.Companion.getInstance().onMessageNewTipUpdateAsync(message);
    }
}
